package cn.online.action;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import cn.online.model.Student;
import cn.online.model.Teacher;
import cn.online.service.StudentService;
import cn.online.service.TeacherService;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	
	private String username;
	private String realname;
	private String password;
	private int type;
	private String repassword;
	private String email;
	private StudentService studentService;
	private TeacherService teacherService;
	
	
	public String getRealname() {
		return realname;
	}
	public void setRealname(String realname) {
		this.realname = realname;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public String getRepassword() {
		return repassword;
	}
	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	public StudentService getStudentService() {
		return studentService;
	}
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}
	public TeacherService getTeacherService() {
		return teacherService;
	}
	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}
	
	public String regedit(){
		if(username==null||realname==null||repassword==null||password==null||!password.equals(repassword))
		{
			return INPUT;
		}else if(type==1){
			Teacher teacher =  teacherService.findByName(username);
			if(teacher==null&&repassword.equals(password)){
				teacher = new Teacher();
				teacher.setTmail(email);
				teacher.setTname(username);
				teacher.setTpassword(password);
				teacher.setTrealname(realname);
				teacherService.insert(teacher);
				return SUCCESS;
			}
			return INPUT;
		}else{
			Student student =  studentService.findByName(username);
			if(student==null&&repassword.equals(password)){
				student = new Student();
				student.setSmail(email);
				student.setSname(username);
				student.setSrealname(realname);
				student.setSpassword(password);
				studentService.insert(student);
				return SUCCESS;
			}
			return INPUT;
		}
	}
	
	
	public String userLogin(){
		if(type==1){
			Student student = studentService.login(username,password);
			if(student!=null){
				HttpSession session = ServletActionContext.getRequest().getSession();
				session.setAttribute("student", student);
				return  SUCCESS;
			}
		}else if(type==0){
			Teacher teacher = teacherService.login(username,password);
			if(teacher!=null){
				HttpSession session = ServletActionContext.getRequest().getSession();
				session.setAttribute("teacher", teacher);
				return SUCCESS;
			}
		}
		return INPUT;
	}
	
	public String userLogout(){
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.invalidate();
		return SUCCESS;
	}
	
	public String getUser(){
		HttpSession session = ServletActionContext.getRequest().getSession();
		if(session.getAttribute("teacher")!=null){
			return "teacher";
		}else if(session.getAttribute("student")!=null){
			return "student";
		}
		return "guest";
	}
}
